﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ircclient
{
    class Smiles
    {
        static string path = "D:/";
        static string patern = @"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*(jpg|png|gif)";
        static RegexOptions option = RegexOptions.Compiled;
        static Regex reg = new Regex(patern,option);
        static string format = "<span class=\"spoiler\"> <a class=\"spoiler-head\" href=\"#\">{0}</a> <img class=\"spoiler-body\" full=\"{0}\"></span> ";

        static public string Parsing(string text)
        {
            
            string str_tmp;
            MatchCollection matches = reg.Matches(text);
            foreach(Match mat in matches)
                text = text.Replace(mat.Value ,String.Format(format,mat.Value));
            

            StreamReader conf = new StreamReader(path + "smiles.txt");
            int i = 1;
            while ((str_tmp = conf.ReadLine()) != null)
            {
                string[] linecfg = str_tmp.Split(',');
                string alt = linecfg[0];
                for (int j = 1; j < linecfg.Count(); j++)
                {
                    text = text.Replace(linecfg[j], String.Format("<img src=\"file://{0}/smiles/{1}.gif\" alt=\"{2}\" >",path,i,alt));
                }
                i++;
            }

            return text;
        }
    }
}
